/*
 * Copyright (c) 2021. ZSDP Inc. All rights reserved.
 */
package com.yff.bpmx.system.feign;

import com.yff.bpmx.common.core.domain.R;
import com.yff.bpmx.common.core.domain.SysForm;
import com.yff.bpmx.common.core.domain.SysRole;
import com.yff.bpmx.common.core.domain.SysUser;
import com.yff.bpmx.common.core.model.LoginUser;
import com.yff.bpmx.common.core.utils.StringUtils;
import com.yff.bpmx.system.service.ISysPermissionService;
import com.yff.bpmx.system.service.ISysRoleService;
import com.yff.bpmx.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Set;

/**
 * 系统服务Feign实现
 *
 * @author zsdp
 */

@ApiIgnore
@RestController
@AllArgsConstructor
public class SysClient implements ISysClient {
    private final ISysUserService sysUserService;
    private final ISysRoleService sysRoleService;
    private final ISysPermissionService permissionService;
//    private final ISysDeployFormService sysDeployFormServicel;

    /**
     * 通过用户名查询用户信息
     *
     * @param username 用户名
     * @param source   请求来源
     * @return 结果
     */
    @Override
    public R<LoginUser> getUserInfo(String username, String source) {
        SysUser sysUser = sysUserService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            return R.fail("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(sysUser.getUserId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser.getUserId());
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSysUser(sysUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        return R.ok(sysUserVo);
    }

    /**
     * 注册用户信息
     *
     * @param sysUser 用户信息
     * @param source  请求来源
     * @return 结果
     */
    @Override
    public R<Boolean> registerUserInfo(SysUser sysUser, String source) {
        return R.ok(sysUserService.registerUser(sysUser));
    }

    /**
     * 查询用户
     *
     * @param id
     * @param source
     * @return
     */
    @Override
    public R<SysUser> selectUserById(Long id, String source) {
        return R.ok(sysUserService.selectUserById(id));
    }

    /**
     * 查询用户列表
     *
     * @param sysUser
     * @param source
     * @return
     */
    @Override
    public R<List<SysUser>> selectUserList(SysUser sysUser, String source) {
        return R.ok(sysUserService.selectUserList(sysUser));
    }

    /**
     * 查询角色
     *
     * @param id
     * @param source
     * @return
     */
    @Override
    public R<SysRole> selectRoleById(Long id, String source) {
        return R.ok(sysRoleService.selectRoleById(id));
    }

    /**
     * 查询角色列表
     *
     * @param source
     * @return
     */
    @Override
    public R<List<SysRole>> selectRoleAll(String source) {
        return R.ok(sysRoleService.selectRoleAll());
    }

    /**
     * 查询表单信息
     *
     * @param id
     * @return
     */
    @Override
    public R<SysForm> selectSysDeployFormByDeployId(String id) {
//        return R.ok(sysDeployFormServicel.selectSysDeployFormByDeployId(id));
        return null;
    }
}
